# coding=utf-8

from utils.SingletonPattern import singleton
from sqlitedbhelper import sqlitedb
from redisdbhelper import RedisDB


@singleton
class DBLayer(object):

    def __init__(self):
        self.__db_keys = ['sqlite', 'redis']
        self.__db_handlers = {}

    def get_sqlite_handler(self):
        if self.__db_keys[0] not in self.__db_handlers.keys():
            self.__db_handlers[self.__db_keys[0]] = sqlitedb()
        return self.__db_handlers[self.__db_keys[0]]

    def get_redis_handler(self, db=0):
        if self.__db_keys[1] + str(db) not in self.__db_handlers.keys():
            self.__db_handlers[self.__db_keys[1]] = RedisDB(db).getRedis()

        return self.__db_handlers[self.__db_keys[1]]
